
Running example of \code{Rectangle}.
\todo{Maybe arrange the code as a top figure, like this: IGJ , OGJ, and at the bottom OIGJ?}
To shorten the code, our rectangle have a single point \code{p}.

Rectangle in IGJ:
\begin{lstlisting}
class Rectangle<I extends ReadOnly> {
  Point<I> p;
  @AssignsFields Rectangle(Point<I> p) {
    this.p = p;
  }
  @ReadOnly boolean equalsTo(Rectangle<ReadOnly> r) {
    return this.p.equalsTo( r.p );
  }
}
\end{lstlisting}
Cons: p is not encapsulated, i.e., it can be changed from the outside:
\begin{lstlisting}
Rectanlge<Mutable> mutR = ...
mutR.getP().x = ...;
\end{lstlisting}
Thus, if \code{Rectangle} had an invariant for upperLeft and lowerRight,
    it could be broken.

Rectangle in OGJ:

\begin{lstlisting}
class Rectangle<O extends World> {
  Point<This> p;
  Rectangle(Point<O> p) {
    this.p = p.clonePoint();
  }
  Point<O> getP() {
    return this.p.clonePoint();
  }
  boolean equalsTo(Rectangle<O> r) {
    return this.p.equalsTo( r.getP() );
  }
}
\end{lstlisting}



Rectangle in OIGJ:
OIGJ can be informally defined as IGJ plus the following rules:
\begin{description}
  \item[Field access] to a \This field, not via \this, changes it to readonly, and
  \item[Field assignment] to a \This field is allowed only via \this.
\end{description}

\begin{lstlisting}
class Rectangle<O extends World,I extends ReadOnly> {
  Point<This,I> p;
  @AssignsFields Rectangle(Point<World,ReadOnly> p) {
    this.p = p.clonePoint();
  }
  @ReadOnly boolean equalsTo(Rectangle<World,ReadOnly> r) {
    return this.p.equalsTo( r.p );
  }
}
\end{lstlisting}
Pros:
- p is encapsulated, invariant cannot be broken
- No need to clone in equalsTo

Example for field access:
Note the difference between: \code{this.p} and \code{r.p}
\begin{lstlisting}
Point<This,I> myP = this.p; // unrestricted access via this
Point<World,ReadOnly> otherP = r.p; // readonly access otherwise
\end{lstlisting}

Example for field assignment:
\begin{lstlisting}
this.p = ...; // legal assignment via this
r.p = ...; // illegal assignment otherwise
\end{lstlisting}


Important: example with caching of point:
\code{\assignable Point<This,I> cache;}
Illegal in IGJ, but legal in OIGJ.
Critical for list iterator where \code{next} is readonly:
\code{\assignable Entry<This,I> current;}
